﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Extensions;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Entity;


namespace TaoPortal.Areas.Admin.Models
{
    public class TrafficModel : BaseModel
    {
        public DateTime Date { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }

        public void Clear(string dateStr)
        {
            List<string> dateList = dateStr.Split(',').ToList();
            var rp = new SystemRepository();
            List<DateTime> dates = new List<DateTime>();
            foreach (var date in dateList)
            {
                dates.Add(Convert.ToDateTime(date));
            }
            rp.ClearTrafficByDate(dates);
        }
        public void Delete(string ids)
        {
            List<string> idList = ids.Split(',').ToList();
            var rp = new SystemRepository();
            rp.ClearTraffic(idList);
        }
        public PagedList<TrafficStatistics> GetTrafficStatistics()
        {
            var rp = new SystemRepository();
            if (StartDate == null && EndDate != null)
                StartDate = EndDate.Value.AddDays(-9);
            if (StartDate != null && EndDate == null)
                EndDate = StartDate.Value.AddDays(9);
            if (StartDate == null && EndDate == null)
            {
                EndDate = DateTime.Now.Date;
                StartDate = DateTime.Now.AddDays(-9).Date;
            }
            var data = rp.GetTrafficStatisticsList(StartDate, EndDate, PageIndex, PageIndex);
            List<TrafficStatistics> list = new List<TrafficStatistics>();
            while (true)
            {
                int count = data.Where(x => x.CreateDate == StartDate).Select(x => x.PV).FirstOrDefault();
                list.Add(new TrafficStatistics { CreateDate = StartDate, PV = count });
                if (StartDate == EndDate)
                    break;
                StartDate = StartDate.Value.AddDays(1);
            }
            return new PagedList<TrafficStatistics>(list.OrderByDescending(x => x.CreateDate).Skip(PageIndex * PageSize).Take(PageSize), PageIndex, PageSize, list.Count);
        }

        public PagedList<TP_Traffic> GetTrafficInfoList()
        {
            var rp = new SystemRepository();
            return rp.GetTrafficList(Date, PageIndex, PageSize);
        }

    }
}